package cn.edu.nju.newsFeed;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Calendar;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import cn.edu.nju.R;
import cn.edu.nju.storage.StoreDataProvider;

import android.app.Activity;
import android.graphics.Bitmap;

public class CommentItem extends FeedItem{
	
	String serverUrl;
	Activity activity;
	
	public CommentItem(long postUserId, long postId, Bitmap avatar, String postUserName,
						String postContent, Calendar time, int likeNumber,Activity activity){
		super(postUserId, postId, avatar, postUserName, postContent, time, likeNumber,activity);
		this.activity = activity;
		serverUrl = activity.getString(R.string.server_url) + "addLike.php";
	}
	
	@Override
	public void addOneLike(){
		try {
			HttpClient client = new DefaultHttpClient();
			HttpContext localContext = new BasicHttpContext();
			HttpPost post = new HttpPost(serverUrl);

			MultipartEntity entity = new MultipartEntity(
					HttpMultipartMode.BROWSER_COMPATIBLE);
			
			entity.addPart("id", new StringBody("" + StoreDataProvider.getInstance(activity).getUserId()));
			entity.addPart("object_id", new StringBody("" + postId));
			entity.addPart("isComment", new StringBody("1"));

			post.setEntity(entity);

			HttpResponse response = client.execute(post, localContext);
			HttpEntity httpEntity = response.getEntity();
			InputStream input = httpEntity.getContent();

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					input));

			String context = reader.readLine();
			reader.close();

			if (context.equals(new String("OK"))) {
				super.addOneLike();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
